﻿
$(function(){
	
})

var $p=new Object();
$p.openWins=new Array();
$p.nextWinNum=0;
$p.winReturnValue=null;
var $web={
	openWin:function(ops){
		$web.setWinReturn(null);
		var wid="";
		if(ops.id)
			wid=ops.id;
		else{
			$p.nextWinNum=$p.nextWinNum+1;
			wid="_MailWinDiv"+$p.nextWinNum;
		}
		var bl=false;
		for(var i=0;i<$p.openWins.length;i++) 
	  {
	  	if($p.openWins[i]==wid){
	  		bl=true;
	  		break;
	  	}
	  }
	  
  	if(!bl){
  		$p.openWins.add(wid);
			var ic="icon_win";
			var doAfterClose;
			if(ops.afterClose)doAfterClose=ops.afterClose;
			else{
				doAfterClose=function(re){}
			}
			if(ops.icon)ic=ops.icon;
			//$("iframe",$('#'+wid)).contents().find("body").empty();
			var closefun=function(){$p.openWins.remove(wid);$("iframe",$('#'+wid)).attr("src","about:blank");if(navigator.userAgent.indexOf("MSIE")>0) {CollectGarbage();}$('#'+wid).window('destroy');$('#'+wid).remove();doAfterClose($web.getWinReturn());};
			$("<div id='"+wid+"'></div>").appendTo(document.body);
			var wintitle=ops.title || '新窗口';
			if(ops.iframe){
				var h=ops.h;
				if(ops.maximized)
					 h=$(window).height();
				$('#'+wid).window({
					title:wintitle,
			    width:ops.w,
			    height:ops.h,
			    modal:ops.modal,
			    inline:true,
			    collapsible:ops.collapsible,
			    minimizable:ops.minimizable,
			    maximizable:ops.maximizable,
			    resizable:ops.resizable,
			    maximized:ops.maximized,
			    content:'<iframe id="'+wid+'_Iframe" name="'+wid+'_Iframe" src="'+ops.url+'" width="100%" height="'+(h-45)+'" frameborder="0" scrolling="no"></iframe>',
			    onClose:closefun
				});
				var st=$(window).scrollTop();
				var h=($(window).height()-ops.h)/2;
				h=h+st;
				$('#'+wid).window("move",{top:h+'px'})
			}else{
				if(ops.url){
					$('#'+wid).window({
						title:wintitle,
				    width:ops.w,
				    height:ops.h,
				    modal:ops.modal,
				    collapsible:ops.collapsible,
				    minimizable:ops.minimizable,
				    maximizable:ops.maximizable,
				    resizable:ops.resizable,
				    maximized:ops.maximized,
				    onClose:closefun
					});
					var st=$(window).scrollTop();
					var h=($(window).height()-ops.h)/2;
					h=h+st;
					$('#'+wid).window("move",{top:h+'px'})
					$('#'+wid).window("refresh",ops.url);
				}else if(ops.html){
					$('#'+wid).window({
						title:wintitle,
				    width:ops.w,
				    height:ops.h,
				    modal:ops.modal,
				    collapsible:ops.collapsible,
				    minimizable:ops.minimizable,
				    maximizable:ops.maximizable,
				    resizable:ops.resizable,
				    maximized:ops.maximized,
				    content:ops.html,
				    onClose:closefun
					});
					var st=$(window).scrollTop();
					var h=($(window).height()-ops.h)/2;
					h=h+st;
					$('#'+wid).window("move",{top:h+'px'})
				}
			}
			
		}else{
  		if(ops.fresh){
					if(ops.iframe)$("iframe",$('#'+wid)).attr("src",ops.url);
					else $('#'+wid).window("refresh",ops.url);
			}
			else $('#'+wid).window("open");
  	}
			
	},
	closeWin:function(wid){
		for(var i=0;i<$p.openWins.length;i++) 
	  {
	  	if($p.openWins[i]==wid){
	  		$("#"+wid).window('close');
	  		break;
	  	}
	  }
	},
	closeCurrWin:function(returnvalue){
		if(returnvalue)
			$web.setWinReturn(returnvalue);
		this.currWin().window('close');
	},
	setWinReturn:function(returnvalue){
		$p.winReturnValue=returnvalue;
	},
	getWinReturn:function(){
		return $p.winReturnValue;
	},
	currWin:function(){
		var cw;
		var czi=0;
		for(i=0;i<$p.openWins.length;i++){
			var zindex=parseInt($("#"+$p.openWins[i]).window("panel").css("zIndex"));
			if(zindex>czi){
				czi=zindex;
				cw=$("#"+$p.openWins[i]);
			}
		}
		return cw;
	},
	execForm:function(ops){
		var mform;
		if(ops && ops.form){
			mform=ops.form;
			if(ops && ops.pageNum)
			$getByName("pageNum",mform).val(ops.pageNum);
			var actionurl=mform.attr("action");
			if(actionurl.indexOf(ctx)!=0)actionurl=ctx+actionurl;
			if(ops && ops.target && ops.target!='undefined'){
				try{ops.target.panel('refresh',{href:ctx+mform.attr("action"),data:mform.serializeArray()});}catch(e){ops.target.panel({href:ctx+mform.attr("action"),data:mform.serializeArray()});}
			}else{
				mform.attr("action",actionurl);
				mform.get(0).submit();
			}
		}
		else{
			mform=$("form");
			if(ops && ops.pageNum)
			$getByName("pageNum",mform).val(ops.pageNum);
			var actionurl=mform.attr("action");
			if(actionurl.indexOf(ctx)!=0)actionurl=ctx+actionurl;
			mform.attr("action",actionurl);
			mform.get(0).submit();
		}
	},
	doForm:function(ops){
		var mform;
		if(ops && ops.form)mform=ops.form;
		else
		 mform=$("form");
		try{
		if(!mform.form("validate"))
			return false;
		}catch(e){}
		var dobackcall=function(json){
			if(json){
				$web._afterupdate(json.jsonpageinfo_);
				if(json && json.resetForm_=='true')mform.get(0).reset();
			}
			if(ops.callback){ops.callback(json);}
		}
		if(mform[0].enctype.toLowerCase()=="multipart/form-data"){
			$web._iframeCallback(mform,dobackcall);
			mform[0].action=ctx+mform.attr("action");
			mform[0].submit();
		}else{
			$.ajax({
				type: 'POST',
				url:ctx+mform.attr("action"),
				data:mform.serializeArray(),
				dataType:"json",
				cache: false,
				success: dobackcall,
				error: $web.ajaxError
			});
		}
		
		return false;
	},
	execUrl:function(url,edata,target){
		if(target && target!='undefined'){
				$.ajax({
					type: 'POST',
					url:url,
					data:edata,
					dataType:"html",
					success: function(re){
						target.html(re);
					}
				});
		}
		else
				window.location.href=url;
	},
	doUrl:function(url,datas,callback){
		var dobackcall=function(json){
			if(json){
				$web._afterupdate(json.jsonpageinfo_);
				if(json && json.resetForm_=='true')mform.get(0).reset();
			}
			if(callback)
				callback(json);
		}
		$.ajax({
			type: 'POST',
			url:url,
			data:datas,
			dataType:"json",
			cache: false,
			success: dobackcall,
			error: $web.ajaxError
		});
		return false;
	},
	_afterupdate: function (jsonpage){
		if(jsonpage){
			if(jsonpage.stateCode=='200'){
				window.parent.location.href=ctx+'/website/index.jsp';
			}
			$(jsonpage).each(function(){
				if(this.script_)
					window.eval(this.script_);
				else if(this.showInfoMsg_)
					$web.infoMsg(this.showInfoMsg_);
				else if(this.showErrorMsg_)
					$web.errorMsg(this.showErrorMsg_);
				else if(this.showAlertMsg_)
					$web.alertMsg(this.showAlertMsg_);
				else if(this.showOkMsg_)
					$web.correctMsg(this.showOkMsg_);
			});
		}
	},
	alertMsg:function(text,title){
		$.messager.alert(title || '系统信息提示',text,'warning');
	},
	
	infoMsg:function(text,title){
		$.messager.show({
				title:title || '系统信息提示',
				msg:"<span class='infoMsgicon'></span>"+text,
				showType:'slide',
				style:{
					right:'',
					top:document.body.scrollTop+document.documentElement.scrollTop,
					bottom:''
				}
			});
	},
	correctMsg:function(text,title){
		$.messager.show({
				title:title || '系统信息提示',
				msg:"<span class='okMsgicon'></span>"+text,
				showType:'slide',
				style:{
					right:'',
					top:document.body.scrollTop+document.documentElement.scrollTop,
					bottom:''
				}
			});
	},
	errorMsg:function(text,title){
		$.messager.alert(title || '系统错误提示',text,'error');
	},
	confirmMsg:function(text,callback,title){
		$.messager.confirm(title || '系统信息提示', text, function(r){
				callback(r);
			});
	},
	showLoad:function(m){
		var msg="正在加载数据...";
		if(m)msg=m;
		if($(".load_shadow").length<=0)
			$('<div class="load_shadow"><div class="load_loadmsg"><table align="center"><tr><td><img src="'+ctx+'/website/default/images/loadicon.gif"></td><td class="msg">'+msg+'</td></tr></table></div></div>').appendTo(document.body);
		$(".load_shadow").css("top",$(window).scrollTop()+'px');
		$(".load_shadow").show();
	},
	hideLoad:function(){
		$(".load_shadow").hide();
	},
	checkLogin:function(requesturl,callback){
		$web.doUrl(ctx+"/wbuser/WbUserPage@checkLogin.page",{},function(re){
			if(re.islogin)callback();
			else{
				$.cookie('jqm_userlogin_url', requesturl,{expires: 1, path: '/'}); 
				window.parent.parent.location.href=ctx+"/website/login.jsp";
			}
		})
	},
	ajaxError:function(xhr, ajaxOptions, thrownError){
		var txt=xhr.responseText; if(txt=='')txt='连接服务器失败！';
		alert(txt);
	},
	_iframeCallback:function(mform,callback){
		var $form = mform, $iframe = $("#callbackframe");
		if ($iframe.size() == 0) {
			$iframe = $("<iframe id='callbackframe' name='callbackframe' src='about:blank' style='display:none'></iframe>").appendTo("body");
		}
		if(!mform[0].ajax) {
			$form.append('<input type="hidden" name="ajax" value="1" />');
		}
		mform[0].target = "callbackframe";
		
		$document = $(document);
	
		$document.trigger("ajaxStart");
		
		$iframe.bind("load", function(event){
			$iframe.unbind("load");
			$document.trigger("ajaxStop");
			
			if ($iframe[0].src == "javascript:'%3Chtml%3E%3C/html%3E';" || // For Safari
				$iframe[0].src == "javascript:'<html></html>';") { // For FF, IE
				return;
			}
	
			var doc = $iframe[0].contentDocument || $iframe[0].document;
	
			// fixing Opera 9.26,10.00
			if (doc.readyState && doc.readyState != 'complete') return; 
			// fixing Opera 9.64
			if (doc.body && doc.body.innerHTML == "false") return;
		   
			var response;
			
			if (doc.XMLDocument) {
				// response is a xml document Internet Explorer property
				response = doc.XMLDocument;
			} else if (doc.body){
				try{
					response = $iframe.contents().find("body").html();
					response = jQuery.parseJSON(response);
				} catch (e){ // response is html document or plain text
					response = doc.body.innerHTML;
				}
			} else {
				// response is a xml document
				response = doc;
			}
			
			callback(response);
		});
	}
}

function IsExt(url,opt)
{  
		var sTemp; 
		var b=false; 
		var s=opt.toUpperCase().split("|");  
		for (var i=0;i<s.length ;i++ ) 
		{ 
			sTemp=url.substr(url.length-s[i].length-1); 
			sTemp=sTemp.toUpperCase();
			s[i]="."+s[i];
			if (s[i]==sTemp)
			{
				b=true;
				break;
			}
		}
		return b;
}

function $getByName(name,parent){
	var obj;
	if(parent){
		return $("[name='"+name+"']",parent);
	}else return $("[name='"+name+"']");
}
function $getById(id,parent){
	var obj;
	if(parent){
		return $("#"+id,parent);
	}else return $("#"+id);
}

function $isMobile(str){
	var isMobile=/^(?:13\d|15\d|18\d)\d{5}(\d{3}|\*{3})$/;
	return isMobile.test(str)
}

function inputNumber(obj){
		$(obj).val($(obj).val().replace(/[^0-9]/g,''));
}
function inputMonery(obj){
		$(obj).val($(obj).val().replace(/[^0-9.]/g,''));
}
$p.openWins.add=function(wid){
	$p.openWins[$p.openWins.length]=wid;
}

$p.openWins.remove=function(wid) 
{ 
    for(var i=0;i<this.length;i++) 
    { 
        if(this[i]==wid){
        	
        	for(var j=(i+1);j<this.length;j++){
        		this[j-1]=this[j];
        		
        	}
        	break
        }
    }
    if(this.length>=1)
    this.length-=1;
}

var funPlaceholder = function(element) {
    var placeholder = '';
    if (element && !("placeholder" in document.createElement("input")) && (placeholder = element.getAttribute("placeholder"))) {
        element.onfocus = function() {
            if (this.value === placeholder) {
                this.value = "";
            }
            this.style.color = '';
        };
        element.onblur = function() {
            if (this.value === "") {
                this.value = placeholder;
                this.style.color = '#999';    
            }
        };
        
        //样式初始化
        if (element.value === "") {
            element.value = placeholder;
            element.style.color = '#999';    
        }
    }
};

//utable
(function($){
 $.fn.uTable = function(args){
	
	return this.each(function() {
		
	});
};

})(jQuery);